<?php

include_once($GLOBALS['cfgPaths']['lib'].'model2/EL.lalr.php');
include_once($GLOBALS['cfgPaths']['lib'].'model2/PSPLexer.o.inc.php');
include_once($GLOBALS['cfgPaths']['lib'].'model2/PSPParser.o.inc.php');
include_once($GLOBALS['cfgPaths']['lib'].'model2/ELLexer.o.inc.php');
include_once($GLOBALS['cfgPaths']['lib'].'model2/ParserMachine.o.inc.php');
include_once($GLOBALS['cfgPaths']['lib'].'model2/ELCallback.o.inc.php');
include_once($GLOBALS['cfgPaths']['lib'].'model2/tagsupport/URL.o.inc.php');
include_once($GLOBALS['cfgPaths']['lib'].'model2/tagsupport/dbaCMSObjectTags.o.inc.php');


/**
 * Action Super Class
 *
 */
class WebAction
{
    
    var $basedir;
    var $env;
    var $dispatcher;

    /**
     * Forwarding to a view. A view is either a flat php file that has access to the 
     * variables $env with the environment wrapper and $actionform, somthing like a bean
     * in Java.
     * 
     * @param view string Name of the view php file (without file extension; the file
     *              must have extension .inc.php) or the view PSP file (with extension 
     *              .psp; file has to have the same name)
     * @param actionform ActionForm Actionform object to transport data to the view
     */
    function forward($view, $actionform)
    {
        if (preg_match('/^[\w\/\.]+$/',$view))
        {
            $env = & $this->env;
            if (preg_match('/\.psp$/', $view))
            {
                $viewparts = explode('/', $view);
                $cmsdba = new dbaCMSObjectTags($GLOBALS['udba']);
                if (file_exists($this->basedir . 'view/'. $view)) 
                {
                    if (file_exists($this->basedir . 'view/'. $view.'.inc.php'))
                    {
                        $cfile = stat($this->basedir . 'view/'. $view.'.inc.php');
                        $sfile = stat($this->basedir . 'view/'. $view);
                        // mtime comparison
                        if ($cfile[9] < $sfile[9])
                            $this->compilePSP($this->basedir . 'view/'. $view, 
                                $this->basedir . 'view/'. $view.'.inc.php');
                    }
                    else
                    {
                        $this->compilePSP($this->basedir . 'view/'. $view, 
                            $this->basedir . 'view/'. $view.'.inc.php');
                    }
                    include($this->basedir . 'view/'.$view.'.inc.php');
                }
                else
                    print ("<b>PSP File not fouuuuund! $view</b></br>");
            }
            else
                include($this->basedir . 'view/'.$view.'.inc.php');
        }
        else
            print ("<strong>View error!</strong>");
    }
    
    /**
     * Compiles the PSP and saves the result for further execution.
     */
    function compilePSP ($sfile, $cfile)
    {
        /*
         * c: core
         * xt: extended
         * s: sql
         * x: xml
         * cms: XICS tags
         * h: html
         * fmt: format
         * p: project (specific) 
         */
        $lex = new PSPLexer(array('c', 'xt', 's', 'x', 'cms', 'h', 'fmt', 'p'), join("", file($sfile)));       
        $lex->lex();
        $parser = new PSPParser($lex, $this->basedir);
        $parser->parse();
        $fp = fopen($cfile, 'w');
        fputs($fp, '<'."?php\n".$parser->getCode()."\n?".">");
        fclose($fp);
    }
    
    /**
     * Helper method to easily set language resorces in the right language to the 
     * actionfor
     */
    function copyResources (& $af, $section, $keys)
    {
        foreach ($keys as $key)
        {
            $af->set($key, $GLOBALS['langres'][$section][$key][$GLOBALS['lang_name']]);
        }
    }

    /**
     * Called by action dispatcher
     */
    function executeAction(& $dispatcher, & $env, $basedir)
    {
        $this->dispatcher = & $dispatcher;
        $this->env = & $env;
        $this->basedir = $basedir;
        $this->execute($env);
    }

    /**
     * abstract
     */
    function execute(& $env)
    {
    }

    function callAction ($actionclass)
    {
        $env = & $this->env;
        $action['file'] = $actionclass;
        preg_match('/(\w+)$/',$actionclass, $matches);
        $action['class'] = $matches[1];
        if (file_exists($this->basedir . '/action/'.$action['file'].$this->dispatcher->fileending))
        {
            include_once($this->basedir . '/action/'.$action['file'].$this->dispatcher->fileending);
            $actionobj = new $action['class'](); 
            $actionobj->executeAction($this->dispatcher, $env, $this->basedir);
        }
    }

}

?>